/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.excesys.exsecs.admin.api.entity.SysUser;
import com.excesys.exsecs.admin.api.feign.RemoteUserService;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.om.constans.OmCommonConstants;
import com.excesys.exsecs.om.dto.ComplainDto;
import com.excesys.exsecs.om.entity.OmWOrderComplain;
import com.excesys.exsecs.om.mapper.OmWOrderComplainMapper;
import com.excesys.exsecs.om.service.OmWOrderComplainService;
import com.excesys.exsecs.om.utils.DateUtils;
import com.excesys.exsecs.om.vo.ComplainExcelVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

//import com.excesys.exsecs.om.utils.DateUtil;

/**
 * 投诉工单表
 *
 * @author weiyj
 * @date 2023-02-24 13:53:35
 */
@Service
@RequiredArgsConstructor
public class OmWOrderComplainServiceImpl extends ServiceImpl<OmWOrderComplainMapper, OmWOrderComplain>
		implements OmWOrderComplainService {

	private final RemoteUserService userService;

	@Override
	public Boolean saveComplain(OmWOrderComplain omWOrderComplain) {
		return SqlHelper.retBool(this.baseMapper.insert(omWOrderComplain));
	}

	/**
	 * 分页查询投诉信息（含有历史 处理信息）
	 * @param page 分页对象
	 * @param omWOrderComplain 参数列表
	 * @return
	 */
	@Override
	public IPage<OmWOrderComplain> getComplainPage(Page page, OmWOrderComplain omWOrderComplain) {
		return baseMapper.getComplainPage(page, omWOrderComplain);
	}

	/**
	 * 分页查询投诉信息（含有历史 处理信息）
	 * @param omWOrderComplain 参数列表
	 * @return
	 */
	@Override
	public List<ComplainExcelVO> getComplainList(OmWOrderComplain omWOrderComplain) {

		List<OmWOrderComplain> complainList = baseMapper.getComplainList(omWOrderComplain);
		// 转换成execl 对象输出
		return complainList.stream().map(orderComplain -> {
			ComplainExcelVO complainExcelVO = new ComplainExcelVO();
			BeanUtil.copyProperties(orderComplain, complainExcelVO);
			return complainExcelVO;
		}).collect(Collectors.toList());
	}

	/**
	 * 投诉操作
	 * @param complainDto 参数列表
	 * @return
	 */
	@Override
	public R<Boolean> operateComplain(ComplainDto complainDto) {

		String complainState = complainDto.getComplainState();
		Boolean isok = true;
		String returnMsg = "";

		OmWOrderComplain omWOrderComplain = new OmWOrderComplain();
		omWOrderComplain.setCpId(complainDto.getCpId());
		omWOrderComplain = this.getOne(new QueryWrapper<>(omWOrderComplain));
		omWOrderComplain.setComplainState(complainState);

		// 指派
		if (complainState.equals(OmCommonConstants.COMPLAIN_STATE_1)) {
			omWOrderComplain.setHandleUserId(complainDto.getHandleUserId());
			R<SysUser> userInfo = userService.getUserById(complainDto.getHandleUserId());
			omWOrderComplain.setHandleUserName(userInfo.getData().getUsername());
			omWOrderComplain.setHandleUserPhone(userInfo.getData().getPhone());
			omWOrderComplain.setComplainUserId(complainDto.getHandleUserId());
			returnMsg = "投诉已指派";
		}
		// 处理
		if (complainState.equals(OmCommonConstants.COMPLAIN_STATE_2)) {
			returnMsg = "投诉已处理";
		}
		// 确认
		if (complainState.equals(OmCommonConstants.COMPLAIN_STATE_3)) {
			omWOrderComplain.setDutyUserId(complainDto.getDutyUserId());
			omWOrderComplain.setConfirmDesc(complainDto.getConfirmDesc());
			returnMsg = "投诉已确认";
		}
		// 反馈
		if (complainState.equals(OmCommonConstants.COMPLAIN_STATE_4)) {
			omWOrderComplain.setFeedbackTime(
					LocalDateTime.parse(DateUtil.now(), DateTimeFormatter.ofPattern(DateUtils.DATE_FORMATE_STRING_A)));
			omWOrderComplain.setFeedbackDesc(complainDto.getFeedbackDesc());
			returnMsg = "投诉已反馈";
		}
		this.getBaseMapper().updateById(omWOrderComplain);
		return R.ok(isok, returnMsg);
	}

}
